<?php

class Dcmp_Template extends Kd_Template{
	
	private $_domain = NULL;
	
	public function __construct($domain){
		$this->_domain = $domain;
		parent::__construct();
		$this->assign('localize_code', $this->getDomain()->getConfig()->getSetting('localizeCode'));
	}
	
	protected function initialize(){
		$this->assignMulti(array(
			'identity'=>Kd_Web::getIdentity(),
			'pub_url'=>Kd_Web::getRouter()->getBaseUrl().'pub/',
                        'skin_name'=>$this->getDomain()->getConfig()->getSetting('skinName'),
                        'skin_url'=>Kd_Web::getRouter()->getBaseUrl().'pub/skin/',
                        'base_url'=>Kd_Web::getRouter()->getBaseUrl(),
			'domain_url'=>Kd_Web::getRouter()->getDomainUrl()
		));
	}
	
	public function getDomain(){
		return $this->_domain;
	}

    public function __get($name) {
        $result = '';
        $name = Kd_Util_String::wordToUnderline($name);
        if (isset($this->arguments[$name])){
            $result = $this->arguments[$name];
        }elseif(Kd_Web::getVariables()->hasData($name)){
            $result = Kd_Web::getVariables()->getData($name);
        }

        return is_string($result) ? urldecode($result) : $result;
    }

    public function setLayout($value) {
        if(!file_exists($value)){
            $value = Kd_Util_IO::findFile($this->getDomain()->getConfig()->getSetting('templateDirectories'), $value);
        }
        parent::setLayout($value);
    }

    public function render($path) {
        ob_start();
        include $path;
        $content = ob_get_clean();
        if (!is_null($this->layout)) {
            if (!empty($this->blockContents)) {
                $this->registeredBlocks = array_merge($this->blockContents, $this->registeredBlocks);
            }
            $template = new Dcmp_Template($this->getDomain());
            $content = $template->setBlockContents($this->registeredBlocks)->render($this->layout);
        }

        return $content;
    }
}

